package com.pai.gui.component;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;

import com.pai.service.TransFileManager;
/**
 * 显示用户信息的列表。列表继承Jcomponent对象。实现个性化。
 * @author Jeff(main)+Vido
 *
 */
public class UserIconList extends JComponent {
	
	private static final long serialVersionUID = 1L;
	//用户信息数据源
	private UserListModel usersource;
	private UserListCell cell;
	private int bz = 0;
	private UserIconList list = this;
	public UserIconList() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.usersource = new UserListModel();
	}
	
	// 所有的控件 在对当前项操作时调用此变量
	private List<JComponent> totalcell = new ArrayList<JComponent>();
	
	// 选中的索引
	private int selectIndex = -1;
	
	// 选中的颜色
	private Color selectColor = new Color(33, 131, 181);
	// 鼠标经过的颜色
	private Color passColor = new Color(99, 99, 99);
	
	public Color getSelectColor() {
		return selectColor;
	}
	public void setSelectColor(Color selectColor) {
		this.selectColor = selectColor;
	}
	public Color getPassColor() {
		return passColor;
	}

	public void setPassColor(Color passColor) {
		this.passColor = passColor;
	}
	
	public UserListModel getUsersource(){
		return usersource;
	}
	/**
	 * 设置用户数据源
	 * @param usermodel
	 */
	public void setUsersource(UserListModel usermodel){
		if(usermodel==null){
			return;
		} else {
			this.usersource.removeSourceRefreshListener(this);
			this.usersource = null;
		}
		this.usersource = usermodel;
		this.usersource.addSourceRefreshListener(this);
		this.refreshData();
	}
	
	public void setCell(UserListCell cell) {
		this.cell = cell;
	}
	
	
	/**
	 * 选中某一行时执行此方法
	 * @param selectIndex
	 */
	public void setSelectIndex(int selectIndex) {
		for (int i = 0; i < totalcell.size(); i++) {
			//所有项设置无背景
			totalcell.get(i).setOpaque(false);
			totalcell.get(i).setBackground(null);
			if (cell != null)
				((UserListCell) totalcell.get(i)).setSelect(false);
		}
		if (selectIndex < totalcell.size() && selectIndex > -1 /*&& bz==0*/) {
			//为选中项设置背景
			bz=1;
			totalcell.get(selectIndex).setOpaque(true);
			totalcell.get(selectIndex).setBackground(list.getSelectColor());
			//if(bz==0){totalcell.get(selectIndex).setBackground(Color.red);bz=1;}

			if (cell != null)
				((UserListCell) totalcell.get(selectIndex)).setSelect(true);
		}

		this.selectIndex = selectIndex;
	}

	public int getSelectIndex() {
		return selectIndex;
	}
	
	/**
	 * 刷新数据
	 */
	public void refreshData() {
		//System.out.println("refresh data");
		if (usersource.getAllCell().size() != 0) {
			//排序
			//Collections.sort(Downloadsource.getAllCell(), Downloadsource.getComparator());
		}
		this.removeAll();
		this.totalcell.clear();
		for (int i = 0; i < usersource.getAllCell().size(); i++) {
			//System.out.println("No."+i);
			JComponent cel = null;
			if (cell != null) {
				try {
					//System.out.println("new instance");
					TransFileManager tfm = cell.getTfm();
					cell = cell.getClass().newInstance();
					cell.setTfm(tfm);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (cell == null) {
				//System.out.println("cell == null");
				cel = new JLabel(usersource.getAllCell().get(i).toString());
				cel.setSize(200, 80);
				cel.setOpaque(false);
//				cell.setBackground(Color.orange);
			} else {
				//System.out.println("cell != null");
				cel = cell.getListCell(this, usersource.getAllCell().get(i));
			}
			//对整个cell添加鼠标监听事件
			cel.addMouseListener(new MouseAdapter() {
				//鼠标点击
				//int bz = 0;
				public void mouseClicked(MouseEvent e) {
					for (int i = 0; i < totalcell.size(); i++) {
						if (e.getSource().equals(totalcell.get(i))) {
							//当前选中项
							//if(bz==0){list.setSelectIndex(i);break;}
							list.setSelectIndex(i);
							//list.setSelectIndex(i+1);
							//if(bz==1) bz=0;
							break;
						}
					}
				}
				//鼠标移入
				public void mouseEntered(MouseEvent e) {
					for (int i = 0; i < totalcell.size(); i++) {
						if (i != list.getSelectIndex()) {
							//非选中项
							if (e.getSource().equals(totalcell.get(i))) {
								totalcell.get(i).setOpaque(true);
								totalcell.get(i).setBackground(list.getPassColor());
								break;
							}
						}
					}
				}
				//鼠标移出
				public void mouseExited(MouseEvent e) {
					JComponent jc = (JComponent) e.getSource();

					if (list.getSelectIndex() < totalcell.size()) {
						if (list.getSelectIndex() != -1) {
							if (!jc.equals(totalcell.get(list.getSelectIndex()))) {
								//非选中项
								jc.setOpaque(false);
								jc.setBackground(null);
							}
						} else {
							jc.setOpaque(false);
							jc.setBackground(null);
						}
					}
				}
			});
			//将cell逐条加入totalcell中,在此为totalcell赋值
			this.totalcell.add(cel);
			this.add(cel);
		}
		if (list.getSelectIndex() != -1
				&& list.getSelectIndex() < usersource.getAllCell().size()) {
			//为选中项设置背景
			totalcell.get(list.getSelectIndex())
					.setBackground(list.getSelectColor());
		}
		this.revalidate();
		this.repaint();
	}

	/**
	 * 源数据更新
	 * @param list
	 */
	public void sourceRefreshEvent(List list) {
		this.refreshData();
	}
	
	
}
